package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


/**
 * 员工管理模块
 */
@RestController
@Api("员工管理模块")
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param employee
     * @param session
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {

        //TODO 1 判断用户是否存在
//        LambdaQueryWrapper<Employee> wrapper = Wrappers.lambdaQuery(Employee.class)
//                .eq(Employee::getUsername, employee.getUsername());
//        Employee logEmployee = employeeService.getOne(wrapper);

        Employee logEmployee = employeeService.lambdaQuery()
                .eq(Employee::getUsername, employee.getUsername())
                .one();

        if (logEmployee == null) {
            return R.error("用户名不存在！");
        }


        // TODO 2 判断密码是否正确
        String pwdMD5 = DigestUtils.md5Hex(employee.getPassword());
        if (!pwdMD5.equals(logEmployee.getPassword())) {
            return R.error("密码错误！");
        }


        //TODO 3 判断状态是否启用
        if (logEmployee.getStatus() == 0) {
            return R.error("当前用户被禁用！");
        }
        session.setAttribute("employee", logEmployee.getId());
        return R.success(logEmployee);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation("员工分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "page",value = "页码"),
                    @ApiImplicitParam(name = "pageSize",value = "每页显示条数"),
                    @ApiImplicitParam(name = "name",value = "员工名称关键字")
            }
    )
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        Page<Employee> mapPage = new Page<>(page, pageSize);
        Page<Employee> page1 = employeeService.lambdaQuery()
                .like(StringUtils.isNotBlank(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime)
                .page(mapPage);
        return R.success(page1);
    }

    /**
     * 退出系统
     *
     * @param session
     * @return
     */
    @ApiOperation("退出登录")
    @PostMapping("logout")
    public R<String> logout(HttpSession session) {
        session.invalidate();
        return R.success("退出成功！");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @param session
     * @return
     */
    @ApiOperation("添加员工")
    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpSession session) {

        employee.setPassword(DigestUtils.md5Hex("123456"));
        Long employee1 = (Long) session.getAttribute("employee");
        employee.setCreateUser(employee1);
        employee.setUpdateUser(employee1);
        employeeService.save(employee);
        return R.success("账号注册成功!");

    }

    /**
     * 查询员工
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        System.out.println("-------id=" + id);
        Employee one = employeeService.lambdaQuery().eq(Employee::getId, id).one();
        return R.success(one);
    }


    /**
     * 编辑员工信息&状态修改
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> disable(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return R.success("success");
    }
}



